tidyverse: ggplot2

้ƒญ่€€ไป

ggplot2

่ผ‰ๅ…ฅ tidyverse

library(tidyverse)

ๅฐ‡ gapminder ่ณ‡ๆ–™่ฎ€ๅ…ฅ R

library(DBI)

con <- dbConnect(RMySQL::MySQL(), 
                 dbname = "gapminder",
                 host = "rsqltrain.ced04jhfjfgi.ap-northeast-1.rds.amazonaws.com",
                 port = 3306,
                 user = "trainstudent",
                 password = "csietrain")

gapminder <- dbReadTable(con, "gapminder")
dbDisconnect(con)
## [1] TRUE

The best stats youโ€™ve ever seen

https://youtu.be/jbkSRLYSojo

็›ธ้—œ๏ผšๆ•ธๅ€ผ vs.ย ๆ•ธๅ€ผ

ggplot() + geom_point() ็นช่ฃฝๆ•ฃไฝˆๅœ–

gapminder_2007 <- gapminder %>%
  filter(year == 2007)
scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

้กฏ็คบๆ•ฃไฝˆๅœ–

scatter

aes() ไธญๅŠ ๅ…ฅ color =

scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point()

ไธๅŒ็š„ๆดฒๅˆฅๆœ‰ไธๅŒ็š„้ก่‰ฒ

scatter

log_scale

scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10()

้กฏ็คบ log ๆ•ธๅ€ผ

scatter

็ทšๅœ–๏ผšๆ™‚้–“ vs.ย ๆ•ธๅ€ผ

ggplot() + geom_line() ็นช่ฃฝ็ทšๅœ–

gapminder_tw <- gapminder %>%
  filter(country == "Taiwan")
line <- ggplot(gapminder_tw, aes(x = year, y = lifeExp)) +
  geom_line()

้กฏ็คบ็ทšๅœ–

line

ๅคšๅ€‹็ทšๆข

gapminder_na <- gapminder %>%
  filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
multi_lines <- ggplot(gapminder_na, aes(x = year, y = lifeExp, color = country)) +
  geom_line()

้กฏ็คบๅคš็ต„็ทšๅœ–

multi_lines

ๆ•ฃไฝˆ

ggplot() + geom_histogram() ็นช่ฃฝ็›ดๆ–นๅœ–

hist <- ggplot(gapminder_2007, aes(x = lifeExp)) +
  geom_histogram(bins = 40)

้กฏ็คบ็›ดๆ–นๅœ–

hist

ggplot() + geom_boxplot() ็นช่ฃฝ็›’้ฌšๅœ–

box <- ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
  geom_boxplot()

้กฏ็คบ็›’้ฌšๅœ–

box

็›ดๆ–นๅœ–ๅŠ ๅ…ฅ facet_wrap()

multi_hists <- ggplot(gapminder_2007, aes(x = lifeExp, fill = continent)) +
  geom_histogram(bins = 20) +
  facet_wrap(~continent, nrow = 2)

้กฏ็คบๅคšๅ€‹็›ดๆ–นๅœ–

multi_hists

ๆŽ’ๅ๏ผˆ้กžๅˆฅ vs.ย ๆ•ธๅ€ผ๏ผ‰

ๅž‚็›ด้•ทๆขๅœ– ggplot() + geom_bar(stat = "identity")

gapminder_2007_na <- gapminder_2007 %>%
  filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
barv <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
  geom_bar(stat = "identity")

้กฏ็คบๅž‚็›ด้•ทๆขๅœ–

barv

ๆฐดๅนณ้•ทๆขๅœ– + coord_flip()

barh <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
  geom_bar(stat = "identity")+ 
  coord_flip()

้กฏ็คบๆฐดๅนณ้•ทๆขๅœ–

barh

ๅœจไธ€ๅ€‹็•ซๅธƒไธŠ็•ซๅคšๅ€‹ๅœ–ๅฝข

  • ไฝฟ็”จ gridExtra ๅฅ—ไปถไพ†ๅนซๅฟ™
  • grid.arrange() ๅ‡ฝๆ•ธ
install.packages("gridExtra")
library(gridExtra)

gg1 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
  geom_bar(stat = "identity")
gg2 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
  geom_bar(stat = "identity")+ 
  coord_flip()

้กฏ็คบ็ถฒๆ ผ็•ซๅธƒ

grid.arrange(gg1, gg2, nrow = 2)

ggplotly() ๅŠ ๅ…ฅไบ’ๅ‹•ๆ€ง

  • ไฝฟ็”จ plotly ๅฅ—ไปถ็š„ ggplotly() ๅ‡ฝๆ•ธ
install.packages("plotly")
library(plotly)
static_gg <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10()

้กฏ็คบ ggplotly ๅœ–ๅฝข

ggplotly(static_gg)

R ่ชž่จ€็š„ไบ’ๅ‹•ๆ€ง่งฃๆณ•

Plotly + Shiny App

Gapminder Replica

ๅฆ‚ไฝ•ๅšๅ‡บไพ†

R ่ชž่จ€ๅ‹•ๆ…‹่ฆ–่ฆบๅŒ–็š„ Hello World - ๅˆฉ็”จ RSeleniumใ€plotly ่ˆ‡ shiny ๆจกไปฟ Hans Rosling ็š„่ฆ–่ฆบๅŒ–ๅคงไฝœ

ggplot2 ๆ–‡ไปถ

http://docs.ggplot2.org/current/index.html

ไฝœๆฅญ

ไฝœๆฅญไธ€

  • ็นช่ฃฝ 2007 ๅนด็š„ๆฐฃๆณกๅœ–๏ผˆsize ่จญ็‚บ pop๏ผ‰

ไฝœๆฅญไบŒ

  • ็นช่ฃฝๆฑๅŒ—ไบžๅœ‹ๅฎถ gdpPercap ็š„็ทšๅœ–